<?php
namespace app\api\model;
use \think\Db;
use \think\Model;
/**
 * 用户模块
 */
class UserOrder extends Model
{
	//获取购物车列表
    public static function lists($user_id=null,$page=1,$length=4) {
		$where = array();
		if (is_null($user_id)) {
            return $where;
        }else{
            $where['user_id'] = $user_id;
        }
	    $data = Db::table('fwshop_cart c')
		->field('c.cart_id, c.is_check, c.goods_count, g.goods_name, g.goods_thums, g.goods_desc, g.shop_price')
		->join('fwshop_goods g','c.goods_id = g.goods_id')
		//->join('fwshop_goods_attributes ga','a.id = w.artist_id')
		->where($where)
		->order('cart_id')
		->limit($page,$length)
		->select()
		;
        $Cache = array();
		$count = 0;
        foreach ($data as $k=>$v) {
            $Cache[$k] = $v;
			$Cache[$k]['count_money'] = $v['shop_price'] * $v['goods_count'] ;//每一种商品总价
			if($v['is_check'] == 1){
				$count += $v['shop_price'] * $v['goods_count'];//统计选中商品总价
			}
        }
        return array('list'=>$Cache,'total_money'=>$count);
    }
	
	//添加商品到购物车
    public static function addressAdd($data) {
       $where = array();
		if (is_null($data['user_id']) || empty($data['user_id'] ||empty($data['goods_id'])) {
            return $where;
        }
		$nums = Db::name('cart')
		->insert($data)
		;
		if($nums == 0){
			return false;
		}else{
			return $nums;
		}
    }
	//修改购物车商品
    public static function addressUpdate($data) {
        $where = array();
		if (is_null($data['cart_id']) || empty($data['cart_id'])) {
            return $where;
        }else{
            $where['cart_id'] = $data['cart_id'];
        }
		$nums = Db::name('cart')
		->where($where)
		->update($data)
		;
		if($nums == 0){
			return false;
		}else{
			return $nums;
		}
    }
	//删除购物车商品$cart_id,$user_id,$is_all_clear
    public static function addressDelete($cart_id=null,$user_id=null,$is_all_clear=0) {
        $where_one = array();
		$where_all = array();
		if (!is_null($cart_id)) {
            $where_one['cart_id'] = $cart_id;
        }else{
			return $where_one;
		}
		if (!is_null($user_id)) {
            $where_all['user_id'] = $user_id;
        }else{
			return $where_all;
		}
		
		if($is_all_clear != 0){
			$nums = Db::name('cart')
			->where($where_all)
			->delete();
		}else{
			$nums = Db::name('cart')
			->where($where_one)
			->delete();
		}
		
		if($nums == 0){
			return false;
		}else{
			return $nums;
		}
    }

	//购物车商品详情
    public static function cartInfo($cart_id=null) {
        $where = array();
		if (is_null($cart_id)) {
            return $where;
        }else{
            $where['cart_id'] = $cart_id;
        }
        $data = Db::name('cart')
		->field('cart_id, user_name, user_tel, user_phone, province_id, city_id, country_id, address, post_code')
		->where($where)
		->find()
		;
        $Cache = array();
        foreach ($data as $k=>$v) {
            $Cache[$k] = $v;
        }
        return $Cache;
    }


}